package com.huawei.esdk.tp.business.professional.terminalconfctrl;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.business.professional.confctrl.ConfCtrlService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.ControlPwd;

public class TerminalConfCtrlService extends BaseService
{
    private static final Logger LOGGER = Logger.getLogger(ConfCtrlService.class);
    
    private SiteInfoCommService siteInfoCommService;
    
    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }
    
    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }

    public SDKErrorCode requestChairEx(String siteUri)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        
        int checkSiteUriErrCode = checkSiteUri(siteUri);
        if (0 != checkSiteUriErrCode)
        {
            result.setErrCode(checkSiteUriErrCode);
            return result;
        }
        //        // 准备Model
        //        HoldingConference holdingConference = new HoldingConference(confId);
        //        // 调用Model
        //        result = holdingConference.requestChair(siteUri);
        
        Site site = new Site();
        site.setUri(siteUri);
        // 调用Model
        result = site.requestChair();
        
        return result;
    }

    public SDKErrorCode releaseChair(String siteUri)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        
        if (StringUtils.isEmpty(siteUri))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORDESC);
            return result;
        }
        
        //        // 准备Model
        //        HoldingConference holdingConference = new HoldingConference(confId);
        //        // 调用Model
        //        result = holdingConference.releaseChair();
        
        // 准备Model
        Site site = new Site();
        site.setUri(siteUri);
        // 调用Model
        result = site.releaseChair();
        
        return result;
    }

    public SDKResult<ControlPwd> queryConfCtrlPwd(String siteUri)
        throws SDKException
    {
        SDKResult<ControlPwd> result = new SDKResult<ControlPwd>();
        
        int checkSiteUriErrCode = checkSiteUri(siteUri);
        if (0 != checkSiteUriErrCode)
        {
            result.setErrCode(checkSiteUriErrCode);
            return result;
        }
        
        // 准备Model
        Site site = new Site();
        site.setUri(siteUri);
        // 调用Model
        result = site.getConfCtrlPsd();
        
        return result;
    }
    
    public SDKResult<SiteInConference> queryBroadInfo(String siteUri)
        throws SDKException
    {
        SDKResult<SiteInConference> result = new SDKResult<SiteInConference>();
        
        int checkSiteUriErrCode = checkSiteUri(siteUri);
        if (0 != checkSiteUriErrCode)
        {
            result.setErrCode(checkSiteUriErrCode);
            return result;
        }
        // 准备Model
        SiteInConference site = new SiteInConference();
        site.setUri(siteUri);
        // 判断是否支持直/录播
        SDKResult<SiteInConference> isSupport = site.isSupportReocrd();
        // 返回值错误时
        if (0 != isSupport.getErrCode())
        {
            result.setErrCode(isSupport.getErrCode());
            return result;
        }
        if (0 == isSupport.getResult().getIsSupportLive() && 0 == isSupport.getResult().getIsSupportRecord())
        {
            site.setRecordBroadStatus(null);
            site.setDirectBroadStatus(null);
            result.setErrCode(isSupport.getErrCode());
        }
        else
        {
            // 调用Model
            SDKResult<SiteInConference> broadStatus = site.queryBroadInfo();
            if (0 != broadStatus.getErrCode())
            {
                result.setErrCode(broadStatus.getErrCode());
                return result;
            }
            site.setDirectBroadStatus(broadStatus.getResult().getDirectBroadStatus());
            site.setRecordBroadStatus(broadStatus.getResult().getRecordBroadStatus());
            result.setErrCode(broadStatus.getErrCode());
        }
        
        site.setIsSupportLive(isSupport.getResult().getIsSupportLive());
        site.setIsSupportRecord(isSupport.getResult().getIsSupportRecord());
        result.setResult(site);
        
        return result;
    }
    
    public SDKErrorCode setRecordBroad(String siteUri, Integer action)
        throws SDKException
    {
        SDKErrorCode resultCode = new SDKErrorCode();
        
        int checkSiteUriErrCode = checkSiteUri(siteUri);
        if (0 != checkSiteUriErrCode)
        {
            resultCode.setErrCode(checkSiteUriErrCode);
            return resultCode;
        }
        if (null == action)
        {
            resultCode.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return resultCode;
        }
        
        // 准备Model
        SiteInConference site = new SiteInConference();
        site.setUri(siteUri);
        // 判断是否支持录播
        SDKResult<SiteInConference> result = site.isSupportReocrd();
        if (0 != result.getErrCode())
        {
            LOGGER.debug("errCode = " + result.getErrCode());
            resultCode.setErrCode(result.getErrCode());
            return resultCode;
        }
        // 调用Model
        if (0 == action)
        {
            if (0 == result.getResult().getIsSupportRecord())
            {
                resultCode.setErrCode(ErrInfo.CONF_IS_NOT_SUPPORT_RECORD_ERRORCODE);
                return resultCode;
            }
            resultCode = site.getStreamOperator().startRecordBroad();
        }
        else if (1 == action)
        {
            if (0 == result.getResult().getIsSupportRecord())
            {
                resultCode.setErrCode(ErrInfo.CONF_IS_NOT_SUPPORT_RECORD_ERRORCODE);
                return resultCode;
            }
            resultCode = site.getStreamOperator().stopRecordBroad();
        }
        else
        {
            resultCode.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
            return resultCode;
        }
        
        return resultCode;
    }
    
    public SDKErrorCode setDirectBroad(String siteUri, Integer action)
        throws SDKException
    {
        SDKErrorCode resultCode = new SDKErrorCode();
        int checkSiteUriErrCode = checkSiteUri(siteUri);
        if (0 != checkSiteUriErrCode)
        {
            resultCode.setErrCode(checkSiteUriErrCode);
            return resultCode;
        }
        if (null == action)
        {
            resultCode.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return resultCode;
        }
        
        // 准备Model
        SiteInConference site = new SiteInConference();
        site.setUri(siteUri);
        // 判断是否支持直播
        SDKResult<SiteInConference> result = site.isSupportReocrd();
        if (0 != result.getErrCode())
        {
            resultCode.setErrCode(result.getErrCode());
            return resultCode;
        }
        // 调用Model
        if (0 == action)
        {
            if (0 == result.getResult().getIsSupportRecord())
            {
                resultCode.setErrCode(ErrInfo.CONF_IS_NOT_SUPPORT_DIRECT_ERRORCODE);
                return resultCode;
            }
            resultCode = site.getStreamOperator().startDirectBroad();
        }
        else if (1 == action)
        {
            if (0 == result.getResult().getIsSupportRecord())
            {
                resultCode.setErrCode(ErrInfo.CONF_IS_NOT_SUPPORT_DIRECT_ERRORCODE);
                return resultCode;
            }
            resultCode = site.getStreamOperator().stopDirectBroad();
        }
        else
        {
            resultCode.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
            return resultCode;
        }
        return resultCode;
    }
    
    /**
     * 
     * 校验传入的会场uri
     * @param siteUri 会场uri
     * @return 0 表示校验通过
     * @throws SDKException 用log记录Exception
     * @throws SDKException
     */
    private int checkSiteUri(String siteUri)
        throws SDKException
    {
        // 判断会场uri是否为空
        if (StringUtils.isEmpty(siteUri))
        {
            return ErrInfo.SITE_URI_ISNULL_ERRORCODE;
        }
        //修改findBus,判空验证
        if(null == siteInfoCommService)
        {
            return ErrInfo.SDK_SYSTEM_ERRORCODE; 
        }
        // 判断会场uri是否存在
        boolean exist = siteInfoCommService.containSite(siteUri);
        if (!exist)
        {
            return ErrInfo.SITE_NOT_EXIST_ERRORCODE;
        }
        // 判断在SDK设备配置列表中是否存在该会场
        exist = siteInfoCommService.containSiteDevice(siteUri);
        if (!exist)
        {
            return ErrInfo.SITE_NOT_EXIST_IN_DEVICEMAP_ERRORCODE;
        }
        return 0;
    }
}
